package com.rzhd.coursepatriarch.common.utils.photo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;

import com.rzhd.coursepatriarch.common.BuildConfig;

import java.io.File;

/**
 * @author yql
 * @date: 2018/8/16.
 */
public class CameraUtils {
    /**
     * 打开相机
     * @param context
     * @param requestCode
     * @return
     */
    public static void openCamera(Activity context, int requestCode, String picturePath){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            /**
             * 指定拍照存储路径
             * 7.0 及其以上使用FileProvider替换'file://'访问
             */
            if (Build.VERSION.SDK_INT>=24){
                //这里的BuildConfig，需要是程序包下BuildConfig。
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        FileProvider.getUriForFile(context.getApplicationContext(),
                                BuildConfig.APPLICATION_ID+".provider",new File(picturePath)));

                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }else{
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(picturePath)));
            }
            context.startActivityForResult(intent, requestCode);
        }
    }
}
